function bind_calendar_event()
{
	$('.calendar input.deadline	').datepicker({
		showOn: "button",
		changeMonth : true,
		dateFormat: 'yy-mm-dd',
		timeFormat: 'hh-mm-ss'
	});
	$('.calendar-content td').click(function(e){
		$('.balloon').hide();
		e.stopPropagation();
		var relativeX = e.pageX - $(this).offset().left + 22;
		var relativeY = e.pageY - $(this).offset().top - 13;
		var balloon = $(this).find('.balloon');
		balloon.css('left',relativeX);
		balloon.css('top',relativeY);
		balloon.show();
	});
	$('html').click(function(){
		$(this).find('.balloon').hide();
	});
	$('.balloon').click(function(e){
		e.stopPropagation();
	});
	$('form.add-milestone').submit(function(e){
		e.preventDefault();
		var div_e = $(this).parents('td');
		var ob = $(this).serializeArray();
		var str = $(this).attr('action');
		$.ajax({
			url: str,
			type: 'POST',
			data: ob,
			success: function(ret) {
				var ret = $.parseJSON(ret);
				if (ret.success)
				{
					$('#tmpl_calendar_element').tmpl(ret.data).appendTo(div_e);
				}else {alert(ret.message);}
			},
			error: function() {
			},
			complete: function() {
			}
		});	
		return false;
	});
	//Finish milestones
}
function check_milestone(event, cbox, cur_user_id)
{
	event.stopPropagation();
	var div_e = $(cbox).parents('div.element');
	$(cbox).attr('disabled', true);
	var mile_id  = $(cbox).val();
	$.ajax({
		url: '/ajax_milestone/update_milestone_status/'+mile_id+'?cur_user_id='+cur_user_id,
		success: function(ret) {
			var data = $.parseJSON(ret);
			if (data.success)
			{
				if (div_e.hasClass('done'))
					div_e.removeClass('done');
				else
					div_e.addClass('done');
			}else {alert(data.message);}
		},
		error: function() {
		},
		complete: function() {
			$(cbox).attr('disabled', false);
		}
	});	
}
function get_edit_milestone(event, a_trigger, mile_id, cur_user_id)
{
	event.preventDefault();
	event.stopPropagation();
	var relativeX = event.pageX - $(a_trigger).offset().left + 22;
	var relativeY = event.pageY - $(a_trigger).offset().top - 13;
	var balloon_wrapper = $(a_trigger).parents('td').find('.balloon-wrapper');
	var add_mile = balloon_wrapper.html();
	$.ajax({
		url: '/ajax_milestone/get_edit_milestone/'+mile_id+'?cur_user_id='+cur_user_id,
		success: function(ret) {
			balloon_wrapper.empty().append(ret);
			balloon_wrapper.parents('.balloon').css('left', relativeX);
			balloon_wrapper.parents('.balloon').css('top', relativeY);
			balloon_wrapper.parents('.balloon').show();
			submit_edit_milestone(a_trigger, mile_id, cur_user_id);
		},
		error: function() {
		},
		complete: function() {
			$('html').click(function(e){
				e.stopPropagation();
				balloon_wrapper.empty().append(add_mile);
			});
			$(a_trigger).parents('td').click(function(e){
				e.stopPropagation();
				balloon_wrapper.empty().append(add_mile);
			});
		}
	});	
	return false;
}
function submit_edit_milestone(a_trigger, mile_id, cur_user_id)
{
	$(a_trigger).parents('td').find('form').submit(function(e){
		e.preventDefault();
		var div_e = $(this).parents('td');
		var str = $(this).attr('action')+'?'+$(this).serialize();
		var old_deadline = $(a_trigger).parents('td').find('input.old-deadline').val();
		$.ajax({
			url: str,
			success: function(ret) {
				var ret = $.parseJSON(ret);
				if (ret.success)
				{
					if (old_deadline == ret.data.deadline)
						$(a_trigger).empty().append(ret.data);
					else
					{
						var date = new Date(ret.data.deadline);
						$(a_trigger).parents('.element').remove();
						var new_div = $('td#jsmark-date-'+date.getDate()+'-'+date.getMonth());
						$('#tmpl_calendar_element').tmpl(ret.data).appendTo(new_div);
					}
				}else {alert(ret.message);}
			},
			error: function() {
			},
			complete: function() {
			}
		});
		return false;
	});
}
function make_calendar(event, time, user_id)
{
	event.stopPropagation();
	var request_url = '';
	var date = '';
	switch (time)
	{
		case 0:
			date = $(event.currentTarget).parents('.controller').find('input.pre-day').val();
			request_url = '/ajax_milestone/get_pre_milestone/'+date+'?user_id='+user_id;
			break;
		case 2:
			date = $(event.currentTarget).parents('.controller').find('input.next-day').val();
			request_url = '/ajax_milestone/get_next_milestone/'+date+'?user_id='+user_id;
			break;
		default:
			break;
	}
	$.ajax({
		url: request_url,
		success: function(ret) {
			var ret = $.parseJSON(ret);
			if (ret.success)
			{
				render_calendar(ret.data, date);
			}else {alert(ret.message);}
		},
		error: function() {
		},
		complete: function() {
		}
	});
	return false;
}
function render_calendar1(milestone, time, current_week, s_date)
{
	var three_weeks_milestone = [];
	var test = [];
	var today = new Date();
	var str='';
	//Get the pivot day
	if (time == 1)
		pivot_day = new Date();
	else if(time == 0)
		str = $('.calendar .controller').find('input.pre-day').val();
	else
		str = $('.calendar .controller').find('input.next-day').val();
	
	if (s_date != undefined)
		str = s_date;
	var pivot_day = new Date(str);
	$('div.calendar-content').empty();
	for (var i = 0; i<3; i++)
	{
		
		//render milestone in a week
		var week = {days:[]};
		for(var j=0; j< 7; j++)
		{
			if (current_week != undefined)
				var date = new Date(str);
			else
				var date = new Date();
			var m_today = 0;
			var itr = 7*i+j;
			if (itr < pivot_day.getDay())
			{
				date.setDate(date.getDate()-date.getDay()+itr);
				test[itr] = {m_date : date.getDate()};
			}
			else if (itr == today.getDay() && pivot_day.getMonth() == today.getMonth() && pivot_day.getDate() == today.getDate())
			{
				date.setDate(today.getDate());
				test[itr] = {m_date : today.getDate()};
				m_today = 1; 
			}
			else
			{
				date.setDate(date.getDate()+itr-date.getDay());//(itr+(7-pivot_day.getDay()))%7);
				//console.log(date);
				test[itr] = {m_date : date.getDate()};
			}
			week.days[j] = {m_date : date.getDate(),
					m_month : date.getMonth()+1, 
					m_year : date.getYear(),
					m_full_year : date.getFullYear(),
					today:m_today, 
					elements:[],
					cur_user_id:1};
			for (var z = 0; z<milestone.length; z++)
			{
				var mile_date = new Date(milestone[z].deadline);
				if (mile_date.getDate()==date.getDate() && mile_date.getMonth()==date.getMonth())
					week.days[j].elements.push(milestone[z]);
			}
		}
		
		$('#tmpl_calendar_week').tmpl(week).appendTo('div.calendar-content');
		//console.log(week);
	}
	var next_date = (str=='') ? new Date() : new Date(str);
	next_date.setDate(next_date.getDate()+21);
	$('.calendar .controller').find('input.next-day').attr('value',next_date.getFullYear()+'-'+(convert_two_digit(next_date.getMonth()+1))+'-'+convert_two_digit(next_date.getDate()));
	
	var pre_date = (str=='') ? new Date() : new Date(str);
	pre_date.setDate(pre_date.getDate()-21);
	$('.calendar .controller').find('input.pre-day').attr('value',pre_date.getFullYear()+'-'+(convert_two_digit(pre_date.getMonth()+1))+'-'+convert_two_digit(pre_date.getDate()));
	bind_calendar_event();
}
function render_calendar(milestone, s_date)
{
	var three_weeks_milestone = [];
	var test = [];
	var today = new Date();
	var str='';
	//Get the pivot day
	
	if (s_date != undefined)
		str = s_date;
	var pivot_day;
	if (s_date != undefined)
		pivot_day = new Date(str);
	else
		pivot_day = new Date();
	$('div.calendar-content').empty();
	for (var i = 0; i<3; i++)
	{
		
		//render milestone in a week
		var week = {days:[]};
		for(var j=0; j< 7; j++)
		{
			if (s_date != undefined)
				var date = new Date(str);
			else
				var date = new Date();
			var m_today = 0;
			var itr = 7*i+j;
			if (itr < pivot_day.getDay())
			{
				date.setDate(date.getDate()-date.getDay()+itr);
				test[itr] = {m_date : date.getDate()};
			}
			else if (itr == today.getDay() && pivot_day.getMonth() == today.getMonth() && pivot_day.getDate() == today.getDate())
			{
				date.setDate(today.getDate());
				test[itr] = {m_date : today.getDate()};
				m_today = 1; 
			}
			else
			{
				date.setDate(date.getDate()+itr-date.getDay());//(itr+(7-pivot_day.getDay()))%7);
				//console.log(date);
				test[itr] = {m_date : date.getDate()};
			}
			week.days[j] = {m_date : date.getDate(),
					m_month : date.getMonth()+1, 
					m_year : date.getYear(),
					m_full_year : date.getFullYear(),
					today:m_today, 
					elements:[],
					users: users,
					cur_user_id:1};
			for (var z = 0; z<milestone.length; z++)
			{
				var mile_date = new Date(milestone[z].deadline);
				console.log(mile_date);
				if (mile_date.getDate()==date.getDate() && mile_date.getMonth()==date.getMonth())
					week.days[j].elements.push(milestone[z]);
			}
		}
		
		$('#tmpl_calendar_week').tmpl(week).appendTo('div.calendar-content');
		//console.log(week);
	}
	$('div.calendar').find('input[name="project_id"]').each(function(){
		$(this).val(project_id);
	});
	
	var next_date = (str=='') ? new Date() : new Date(str);
	next_date.setDate(next_date.getDate()+21);
	$('.calendar .controller').find('input.next-day').attr('value',next_date.getFullYear()+'-'+(convert_two_digit(next_date.getMonth()+1))+'-'+convert_two_digit(next_date.getDate()));
	
	var pre_date = (str=='') ? new Date() : new Date(str);
	pre_date.setDate(pre_date.getDate()-21);
	$('.calendar .controller').find('input.pre-day').attr('value',pre_date.getFullYear()+'-'+(convert_two_digit(pre_date.getMonth()+1))+'-'+convert_two_digit(pre_date.getDate()));
	bind_calendar_event();
}
